using System;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;

namespace My.IdlConverter.Util
{
    public class ReflectHelper
    {
        public static string ResolvePathByAssembly(Assembly assembly, string relativePath)
        {
            string codebase = assembly.CodeBase;
            if (codebase.IndexOf("file:///") != -1)
                codebase = codebase.Remove(0, "file:///".Length);
            int index = codebase.LastIndexOf('/');
            string fullPath = System.IO.Path.Combine(codebase.Substring(0, index), relativePath);

            return fullPath;
        }

        public static System.Type ClassForName(string name)
        {
            TypeQualifiedName parsedName = TypeNameParser.Parse(name);
            System.Type result = TypeFromAssembly(parsedName);
            if (result == null)
            {
                throw new TypeLoadException("Could not load type '" + parsedName + "', check that type and assembly names are correct");
            }
            return result;
        }

        public static Type TypeFromAssembly(TypeQualifiedName qname)
        {
            try
            {
                Type type = Type.GetType(qname.Type);

                if (type != null)
                {
                    return type;
                }

                if (qname.Assembly == null)
                {
                    return null;
                }

                Assembly assembly = Assembly.Load(qname.Assembly);

                if (assembly == null)
                {
                    return null;
                }

                type = assembly.GetType(qname.Type);

                if (type == null)
                {
                    return null;
                }

                return type;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
